package com.roc.admin.interfaces.controller;

import com.roc.admin.application.service.DeptService;
import com.roc.admin.interfaces.common.Result;
import com.roc.admin.interfaces.dto.DeptDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门控制器
 */
@Tag(name = "部门管理", description = "部门管理相关接口")
@RestController
@RequestMapping("/dept")
public class DeptController {

    private final DeptService deptService;

    public DeptController(DeptService deptService) {
        this.deptService = deptService;
    }

    @Operation(summary = "创建部门")
    @PostMapping
    public Result<Long> createDept(@Validated @RequestBody DeptDTO deptDTO) {
        Long deptId = deptService.createDept(deptDTO);
        return Result.success(deptId);
    }

    @Operation(summary = "更新部门")
    @PutMapping
    public Result<Boolean> updateDept(@Validated @RequestBody DeptDTO deptDTO) {
        boolean success = deptService.updateDept(deptDTO);
        return Result.success(success);
    }

    @Operation(summary = "删除部门")
    @DeleteMapping("/{deptId}")
    public Result<Boolean> deleteDept(@Parameter(description = "部门ID") @PathVariable Long deptId) {
        boolean success = deptService.deleteDept(deptId);
        return Result.success(success);
    }

    @Operation(summary = "获取部门信息")
    @GetMapping("/{deptId}")
    public Result<DeptDTO> getDeptById(@Parameter(description = "部门ID") @PathVariable Long deptId) {
        DeptDTO deptDTO = deptService.getDeptById(deptId);
        return Result.success(deptDTO);
    }

    @Operation(summary = "获取部门列表")
    @GetMapping("/list")
    public Result<List<DeptDTO>> getDeptList() {
        List<DeptDTO> deptDTOList = deptService.getDeptList();
        return Result.success(deptDTOList);
    }

    @Operation(summary = "获取部门树")
    @GetMapping("/tree")
    public Result<List<DeptDTO>> getDeptTree() {
        List<DeptDTO> deptTree = deptService.getDeptTree();
        return Result.success(deptTree);
    }

    @Operation(summary = "根据角色ID获取部门树")
    @GetMapping("/tree/role/{roleId}")
    public Result<List<DeptDTO>> getDeptTreeByRoleId(@Parameter(description = "角色ID") @PathVariable Long roleId) {
        List<DeptDTO> deptTree = deptService.getDeptTreeByRoleId(roleId);
        return Result.success(deptTree);
    }
}
